#include "SoundFun.h"
#include "SimpleAudioEngine.h"
#include "Game/Script/utility.h"

namespace SoundFun
{
	static bool b_effect = true;
	static bool b_music = true;

	static const std::string s_soundPath = "Audio/";
	static const std::string defaultName = "backMusic.mp3";

	void SetSoundBackground(bool fValue)
	{
		b_music = fValue;
		if (b_music)
			playBackMusic(defaultName);
		else
			stopBackMusic();
	}
	void SetSoundEffect(bool fValue)
	{
		b_effect = fValue;
	}
	void playBackMusic(std::string kName)
	{
		if (b_music)
		{
			kName = s_soundPath + kName;
			CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(kName.c_str(), true);
		}
	}
	void stopBackMusic()
	{
		CocosDenshion::SimpleAudioEngine::getInstance()->stopBackgroundMusic();	
	}

	void playEffect(std::string kName,bool bPreload)
	{
		if (!b_effect)
		{
			return;
		}
		kName = s_soundPath + kName;
		if (bPreload)
		{
			CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect(kName.c_str());
		}
		else
		{
			CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(kName.c_str());
		}
	}
	void playEffectDirect(std::string kName,bool bPreload )
	{
		if (!b_effect)
		{
			return;
		}
		if (bPreload)
		{
			CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect(kName.c_str());
		}
		else
		{
			CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(kName.c_str());
		}
	}
	void playCardByGender(int iGender,std::string kName,bool bPreload)
	{
		if (iGender)
		{
			kName = "F_" + kName;
		}
		else
		{
			kName = "M_" + kName;
		}
		playEffect(kName,bPreload);
	}
	void PreloadEffectByGender(std::string kName)
	{
		playCardByGender(0,kName,true);
		playCardByGender(1,kName,true);
	}
	void playCustomEffect(std::string kName)
	{
		if (!b_effect)
		{
			return;
		}
		
		CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(kName.c_str());
	}

	void init()
	{
		b_effect = cocos2d::UserDefault::getInstance()->getBoolForKey("JDNN_EFFECT_VALUE", true);
		b_music = cocos2d::UserDefault::getInstance()->getBoolForKey("JDNN_SOUND_VALUE", true);
	}

} 
